package sedh.project

import grails.plugins.springsecurity.Secured;

class ServicoController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }
	def searchServico = {
		def list = Servico.findAllByNomeIlike(wrapSearchParm(params.searchvalue), [sort:'nome',order:'asc'])
		render(template:'searchResult', model:[searchresults : list])
	}
	def String wrapSearchParm(value) {
		'%'+value+'%'
	   }
	
	@Secured(['IS_AUTHENTICATED_REMEMBERED'])
    def list = {
		def mun = params.get("mun") ? params.get("mun") : 1
		
		params.sort = "nome"
		params.order = "asc"
		
		def servicoInstance = Servico.get(params.id)
		def alocMunSerInstance = AlocadorMunSer.getAll()
		

		if (!servicoInstance) {
			
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'servico.label', default: 'Servico'), params.id])}"
			if(flash.message.contains("1")) flash.message = ""
				[servicoInstanceList: Servico.list(params), servicoInstanceTotal: Servico.count()]
	
		}
		
		else {
			
				[mun:mun, alocMunSerInstance:alocMunSerInstance, servicoInstance: servicoInstance, servicoInstanceList: Servico.list(params), servicoInstanceTotal: Servico.count()]
		
		}
		
    }
	
	@Secured(['IS_AUTHENTICATED_REMEMBERED'])
    def create = {
        def servicoInstance = new Servico()
        servicoInstance.properties = params
        return [servicoInstance: servicoInstance]
    }

    def save = {
		params.nome = params.nome.toLowerCase().split(' ').collect{ it.capitalize() }.join(' ')
        def servicoInstance = new Servico(params)
        if (servicoInstance.save(flush: true)) {
			
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'servico.label', default: 'Servico'), servicoInstance.id])}"
            redirect(action: "show", id: servicoInstance.id)
        }
        else {
            render(view: "create", model: [servicoInstance: servicoInstance])
        }
    }
	
	@Secured(['IS_AUTHENTICATED_REMEMBERED'])
    def show = {
       redirect(action: "list", id: params.id)
    }

	@Secured(['IS_AUTHENTICATED_REMEMBERED'])
    def edit = {
        def servicoInstance = Servico.get(params.id)
        if (!servicoInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'servico.label', default: 'Servico'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [servicoInstance: servicoInstance]
        }
    }

    def update = {
        def servicoInstance = Servico.get(params.id)
        if (servicoInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (servicoInstance.version > version) {
                    
                    servicoInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'servico.label', default: 'Servico')] as Object[], "Another user has updated this Servico while you were editing")
                    render(view: "edit", model: [servicoInstance: servicoInstance])
                    return
                }
            }
            servicoInstance.properties = params
            if (!servicoInstance.hasErrors() && servicoInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'servico.label', default: 'Servico'), servicoInstance.id])}"
                redirect(action: "show", id: servicoInstance.id)
            }
            else {
                render(view: "edit", model: [servicoInstance: servicoInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'servico.label', default: 'Servico'), params.id])}"
            redirect(action: "list")
        }
    }

	@Secured(['IS_AUTHENTICATED_REMEMBERED'])
    def delete = {
		
        def servicoInstance = Servico.get(params.id)
		
		def servicosAlocados = AlocadorMunSer.findAllByServico(servicoInstance)
		
		
		//deleta a alocacao
		for(AlocadorMunSer aloc : servicosAlocados) {
	        if (aloc) {
				
	            try {
	                aloc.delete(flush: true)
	                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'servico.label', default: 'Servico'), params.id])}"
	                
	            }
	            catch (org.springframework.dao.DataIntegrityViolationException e) {
	                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'servico.label', default: 'Servico'), params.id])}"
	                
	            }
	        }
		}
        		//deleta o servico
		if (servicoInstance) {
			
			try {
				servicoInstance.delete(flush: true)
				flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'servico.label', default: 'Servico'), params.id])}"
				
			}
			catch (org.springframework.dao.DataIntegrityViolationException e) {
				flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'servico.label', default: 'Servico'), params.id])}"
				redirect(action: "show", id: params.id)
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'servico.label', default: 'Servico'), params.id])}"
			
		}
		
		redirect(action: "list", id: params.id)
    }
}
